package utils
{
	import flash.events.DataEvent;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.FileReference;
	import flash.net.FileReferenceList;
	import flash.net.URLRequest;
	import flash.utils.Dictionary;
	
	import common.entities.FileInfo;

	public class UploadFileUtil
	{
		/**
		 * 上传多个文件
		 * @param p_progress 进度函数格式  function(uploadedBytes:Number, totalBytes:Number)
		 * @param p_completeDataHandler 完成后回传数据函数 function(Array[FileInfo])
		 * @param p_typeFilter
		 * @return
		 */
		public static function upload(p_progress:Function, p_completeDataHandler:Function, p_typeFilter:Array = null):void
		{
			var files:FileReferenceList = new FileReferenceList();
			files.addEventListener(Event.SELECT, selectedHandler);
			files.browse(p_typeFilter);
			//文件总大小
			var totalBytes:Number = 0;
			//已上传的字节大小
			var uploadedBytes:Number = 0;
			var fileDir:Dictionary = new Dictionary();
			var length:int = 0;
			//选中要上传的文件集合后开始上传
			function selectedHandler(p_evt:Event):void
			{
				var files:FileReferenceList = p_evt.currentTarget as FileReferenceList;
				files.removeEventListener(Event.SELECT, selectedHandler);
				var fileList:Array = files.fileList;
				length = fileList.length;
				for(var i:int = 0;i < fileList.length;i++){
					var file:FileReference = fileList[i] as FileReference;
					var fileInfo:FileInfo = new FileInfo();
					fileInfo.name = file.name;
					fileInfo.size = file.size;
					fileDir[file] = fileInfo;
					totalBytes += file.size;
					file.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
					file.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
					file.addEventListener(ProgressEvent.PROGRESS, progressHandler);
					file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, uploadCompleteHandler);
					file.upload(new URLRequest(".upload"));
				}
			}
			//显示上传进度
			function progressHandler(p_evt:ProgressEvent):void
			{
				var file:FileReference = p_evt.currentTarget as FileReference;
				if(p_progress != null && fileDir[file]){
					var fileInfo:FileInfo = fileDir[file] as FileInfo;
					uploadedBytes += p_evt.bytesLoaded - fileInfo.bytesLoaded;
					fileInfo.bytesLoaded = p_evt.bytesLoaded;
					p_progress(uploadedBytes, totalBytes);
				}
			}
			//上传完成后获取文件存储位置信息
			function uploadCompleteHandler(p_evt:DataEvent):void
			{
				var file:FileReference = p_evt.currentTarget as FileReference;
				removeEvents(file);
				if(fileDir[file]){
					var fileInfo:FileInfo = fileDir[file] as FileInfo;
					fileInfo.resolvePath = p_evt.data;
				}
				length--;
				if(length <= 0 && p_completeDataHandler != null){
					var files:Array = [];
					for each(var f:FileInfo in fileDir){
						files.push(f);
					}
					p_completeDataHandler(files);
				}
			}
			//安全沙箱错误
			function securityErrorHandler(p_evt:SecurityErrorEvent):void
			{
				removeEvents(p_evt.currentTarget as FileReference);
			}
			//写入文件错误
			function ioErrorHandler(p_evt:IOErrorEvent):void
			{
				removeEvents(p_evt.currentTarget as FileReference);
			}
			//移除事件
			function removeEvents(p_file:FileReference):void
			{
				p_file.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
				p_file.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
				p_file.removeEventListener(ProgressEvent.PROGRESS, progressHandler);
				p_file.removeEventListener(DataEvent.UPLOAD_COMPLETE_DATA, uploadCompleteHandler);
			}
		}
	}
}